package com.example.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.example.foodapp.adapter.MyAdapter;
import com.example.foodapp.bean.Food;
import com.example.foodapp.util.OkHttpUtil;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MenuListActivity extends AppCompatActivity {
    ListView lvMenu;
    String returns;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_list);

        List<Food> list=new ArrayList<>();
        lvMenu = findViewById(R.id.lv_menu);


        new Thread(new Runnable() {
            @Override
            public void run() {
                String data = null;
                try {
                    data = OkHttpUtil.getOkHttpUtil().sendPostReq("/foodserver/food/list", "");

                    String finalData = data;
                    Gson gson = new Gson();
                    Food food = gson.fromJson(data.toString(), Food.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "执行至此", Toast.LENGTH_SHORT).show();
                            Log.e("TAG", "run:aaaaaaaaaaaaaaa "+ finalData);
                            lvMenu.setAdapter(new MyAdapter(food.getList(),getApplicationContext()));
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }


}